package com.zzz.controller;

import com.alibaba.excel.EasyExcel;
import com.zzz.dao.ExcelDao;
import com.zzz.dto.ExcelDTO;
import com.zzz.service.TestServiceImpl;
import com.zzz.utils.ExcelDataListener;
import com.zzz.utils.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhuzhizun
 * @date 2021/7/30
 */
@RestController
public class TestController {

    private static final String PATH = "D:\\";

    @Autowired
    private TestServiceImpl testService;

    @Autowired
    private ExcelDao excelDao;

    @Bean(value = "zzz")
    public User getUser(){
        return new User("zzz");
    }

    @PostMapping("/test")
    public void test(@RequestBody User user){
        System.out.println(SpringUtils.getObject("zzz"));
        BeanDefinition beanDefine = SpringUtils.getBeanDefine("zzz");
        System.out.println(beanDefine.getAttribute("age"));
        testService.getString(user.name);

    }


    public static void main(String[] args) {
        if ("△".equals("1111")){
            System.out.println("111");
        }
    }

    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file, @RequestParam("status") Integer status) throws IOException {
        if (file.isEmpty()) {
            return "上传失败，文件为空";
        }
        //得到上传文件全名
        String fileName = file.getOriginalFilename();
        //上传文件后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        //设置可以上传文件的类型
        String []type = {".xls",".xlsx"};
        List<String> fileType = Arrays.asList(type);
        //判断上传的文件 是否属于可以上传的类型
        if (!fileType.contains(suffixName)){
            return "上传失败,文件类型不符合";
        }
        // 有个很重要的点 DemoDataListener 不能被spring管理，要每次读取excel都要new,然后里面用到spring可以构造方法传进去
        // 写法1：
//        String fileName = PATH+ "simpleWrite.xls";
        // 这里 需要指定读用哪个class去读，然后读取第一个sheet 文件流会自动关闭
        EasyExcel.read(file.getInputStream(), ExcelDTO.class, new ExcelDataListener(excelDao,status)).sheet().doRead();
        return "上传成功";
    }

}
